<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'btn:add:xianchangqianzheng'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增签证记录') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.name }}
        </a>
      </template>

      <template #projectno="{ record }">
        <!-- <a @click="handleDetailsForm(record)" >
          <span style="text-decoration: underline;margin-right: 5px;">查看</span>
        </a>
        {{ record.projectcode }} -->
        <a @click="handleDetailsForm(record)" >
          {{ record.projectcode }}
        </a>
        
      </template>
      <template #uploadFile1="{ record }">
        <BasicUpload
          v-model:value="record.dataMap"
          disabled
          :bizKey="record.id"
          :bizType="'PmVisaXc_image'"
          :uploadType="'image'"
          :loadTime="record.__t"
          :showPreview="true"
          :isPreview="false"
          :emptyHidePreview="true"
        />
      </template>
      <template #uploadFile2="{ record }">
        <BasicUpload
          v-model:value="record.dataMap"
          disabled
          :bizKey="record.id"
          :bizType="'PmVisaQz_image'"
          :uploadType="'image'"
          :loadTime="record.__t"
          :showPreview="true"
          :isPreview="false"
          :emptyHidePreview="true"
        />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsDesignVisaPmVisaList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { pmVisaDelete, pmVisaListData } from '/@/api/design/visa/pmVisa';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import { BasicUpload } from '/@/components/Upload';
  import InputForm from './form.vue';

  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const { t } = useI18n('design.visa.pmVisa');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('现场签证管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('项目编号'),
        field: 'projInfo.projectno',
        component: 'Input',
      },
      {
        label: t('项目名称'),
        field: 'projInfo.projectname',
        component: 'Input',
      },
      {
        label: t('上传人'),
        field: 'operator',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
   
    {
      title: t('项目编号'),
      dataIndex: 'projectcode',
      key: 'a.projectcode',
      sorter: true,
      width: 180,
      align: 'left',
      slot: 'projectno',
    },
    {
      title: t('项目名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 240,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('创建时间'),
      dataIndex: 'createDate',
      key: 'a.createtime',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('上传人'),
      dataIndex: 'operator',
      key: 'a.operator',
      sorter: true,
      width: 120,
      align: 'left',
    },

    {
      title: t('签证描述'),
      dataIndex: 'describe',
      key: 'a.describe',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('现场照片'),
      dataIndex: 'scenefile',
      key: 'a.scenefile',
      sorter: true,
      width: 130,
      align: 'center',
      slot: 'uploadFile1',
    },
    {
      title: t('签证照片'),
      dataIndex: 'scenefile',
      key: 'a.scenefile',
      sorter: true,
      width: 130,
      align: 'center',
      slot: 'uploadFile2',
    },
    {
      title: t('签证类型'),
      dataIndex: 'istemporary',
      key: 'a.istemporary',
      sorter: true,
      width: 110,
      align: 'center',
      dictType: 'zf_visa_type',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑现场签证'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'btn:edit:xianchangqianzheng',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除现场签证'),
        popConfirm: {
          title: t('是否确认删除现场签证'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'design:visa:pmVisa:delete',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: pmVisaListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
  });

  function handleDetailsForm(record: Recordable) {
    router.push({
      path: '/proj/info/pmProjectinformation/detailsForm',
      query: {
        id: record.projInfo.id,
        code: record.projectcode,
        tabTitle: `项目详情(${record.projectcode})`,
      },
    });
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await pmVisaDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  onMounted(async () => {
    handleProjecte();
  });
  async function handleProjecte() {
    await getForm().setFieldsValue({
      'projInfo.projectno': userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      'projInfo.projectno': userStore.getProjecte.code,
    });
  }
</script>
